package cn.cw.myupdatedemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import cn.cw.myupdatedemo.patch.PatchUtil;

/**
 * @author zzj15_hqeovpj
 */
public class ThirdActivity extends AppCompatActivity {

    private static final String PATH = Environment.getExternalStorageDirectory().getPath();
    private static final String NEW_APK = PATH + File.separator + "new.apk";
    private static final String OUT_APK = PATH + File.separator + "out.apk";
    private static final String PATCH_FILE = PATH + File.separator + "patch";
    private static final String TAG = "ThirdActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        Log.e(TAG, "path:" + PATH);
        Button buttonMake = findViewById(R.id.btn_make);
        buttonMake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        make();
                    }
                }).start();

            }
        });

        Button buttonDiff = findViewById(R.id.btn_diff);
        buttonDiff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        diff();
                    }
                }).start();

            }
        });
        TextView textView = findViewById(R.id.text_content);
        textView.setText("hello kim!");
    }

    private void diff() {
        if (PatchUtil.diff(this, NEW_APK, PATCH_FILE)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ThirdActivity.this, getString(R.string.diff_done) + PATCH_FILE, Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ThirdActivity.this, getString(R.string.diff_failed), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private void make() {
        if (PatchUtil.make(this, OUT_APK, PATCH_FILE)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ThirdActivity.this, getString(R.string.make_done) + OUT_APK, Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(new File(OUT_APK)),
                            "application/vnd.android.package-archive"));
                }
            });

        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ThirdActivity.this, getString(R.string.make_failed), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
